import http from '@/common/common'
const state = {
	userinfo: {},
	address: [],
	opt: 1
}
//改变state 状态
const mutations = {
	SAVE_USERINFO(state, data) {
		state.userinfo = data || {}
	},
	SAVE_ADDRESS(state, data) {
		state.address = data
	}
}
const actions = {
	login({ commit }, form) {
		return new Promise((resolve, reject) => {
			http.requests('/api/user/login', form).then(res => {
				if(res.data.code == 1){
					var data = res.data.data.userinfo;
					uni.setStorageSync('mobile', data.mobile)
					uni.setStorageSync('token', data.token);
					uni.setStorageSync('uid', data.id);
					uni.setStorageSync('avatar', data.avatar);
					uni.setStorageSync('nickname', data.nickname);
					uni.setStorageSync('noavatar', '../../static/image/bg_img.png');
					uni.setStorageSync('userinfo', data);
					commit('SAVE_USERINFO', data)
				}
				resolve(res)
			}).catch(e => {
				reject(e)
			})
		})
	},
	logout({ commit }) {
            uni.removeStorageSync('token')
			uni.removeStorageSync('mobile')
			uni.removeStorageSync('token')
			uni.removeStorageSync('uid')
			uni.removeStorageSync('userinfo')
			uni.removeStorageSync('avatar')
			commit('SAVE_USERINFO')
			uni.redirectTo({
			    url: '/pages/login/login'
			});
	},
	inituser({ commit }) {
		commit('SAVE_USERINFO', uni.getStorageSync('userinfo'))
	}
}
//暴露核心对象
export default {
	state,
	mutations,
	actions
}